package com.jumi.microservice.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.DateUtil;
import com.jumi.microservice.common.utils.EnumUtil;
import com.jumi.microservice.dto.admin.OrderRequest;
import com.jumi.microservice.dto.admin.OrderResponse;
import com.jumi.microservice.entity.CouponOrder;
import com.jumi.microservice.enumerate.CouponOrderStatusEnum;
import com.jumi.microservice.enumerate.SourceEnum;
import com.jumi.microservice.mapper.CouponOrderMapper;
import com.jumi.microservice.service.ICpsOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ICpsOrderServiceImpl implements ICpsOrderService {
    @Resource
    CouponOrderMapper couponOrderMapper;

    @Override
    public TableDataInfo<OrderResponse> getOrderAll(OrderRequest request) {
        TableDataInfo<OrderResponse> tableDataInfo = new TableDataInfo<>();
        QueryWrapper<CouponOrder> couponOrderQueryWrapper = new QueryWrapper<>();
        if (request.getSource() != null) {
            couponOrderQueryWrapper.eq("source", request.getSource());
        }
        if (request.getGoodsTitle() != null) {
            couponOrderQueryWrapper.like("goods_title", request.getGoodsTitle());
        }
        if (request.getOrderSn() != null) {
            couponOrderQueryWrapper.eq("order_sn", request.getOrderSn());
        }
        if (request.getStartTime() != null && request.getEndTime() != null) {
            couponOrderQueryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        if (request.getOrderStatus() != null) {
            couponOrderQueryWrapper.eq("order_status", request.getOrderStatus());
        }
        couponOrderQueryWrapper.orderByDesc("create_time");
        IPage<CouponOrder> couponOrderIPage = new Page<>(request.getPageNum(), request.getPageSize());
        couponOrderIPage = couponOrderMapper.selectPage(couponOrderIPage, couponOrderQueryWrapper);
        List<OrderResponse> orderResponseList = new ArrayList<>();
        couponOrderIPage.getRecords().forEach(
                couponOrder -> {
                    OrderResponse orderResponse = new OrderResponse();
                    BeanUtils.copyProperties(couponOrder, orderResponse);
                    orderResponse.setCreateTime(DateUtil.format(couponOrder.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                    if (couponOrder.getSettlementTime() != null) {
                        orderResponse.setSettlementTime(DateUtil.format(couponOrder.getSettlementTime(), "yyyy-MM-dd HH:mm:ss"));
                    }
                    orderResponse.setSettlementAmount(couponOrder.getPayAmount());
                    orderResponse.setJmOrderStatus(EnumUtil.getByCode(couponOrder.getCouponOrderStatus(), CouponOrderStatusEnum.class).getMessage());
                    orderResponse.setSource(EnumUtil.getByCode(couponOrder.getSource(), SourceEnum.class).getMessage());
                    orderResponse.setPidName(couponOrder.getPromotionName());
                    orderResponse.setGoodsPrice(couponOrder.getGoodsPrice());
                    orderResponseList.add(orderResponse);
                }
        );
        tableDataInfo.setRows(orderResponseList);
        tableDataInfo.setTotal(couponOrderIPage.getTotal());
        return tableDataInfo;
    }
}
